package com.hans.scala.collection

import scala.collection._


object CollectionTest {
  def testForeach() {
    val greetStrings = new Array[String](3)
    greetStrings(0) = "Hello"
    greetStrings(1) = ", "
    greetStrings(2) = "world!\n"
    for (msg <- greetStrings) {
      print(msg)
    }
    for (i <- 0 to 2) {
      print(i + " ")
    }
    println()

    for (i <- 0.to(1)) {
      print(greetStrings.apply(i))
    }
    println()
    val oneTwoThree = 1 :: 2 :: 3 :: Nil
    println(oneTwoThree)
  }

  def testTuple() {
    val pair = (23, "Tom")
    println(pair)
    println(pair._1)
    println(pair._2)
    val tuple6 = ('u', 'r', "the", 1, 4, "me")
    println(tuple6)
  }

  def testSet() {
    var set = Set("hans", "zjt")
    set += "zx"
    println(set.contains("zx"))
  }

  def testMutableSet() {
    val set = mutable.Set("hans", "zjt")
    //    set += "zx"
    set.+=("zx")
    println(set.contains("zx"))
  }

  def testHashSet() {
    val set = mutable.HashSet("hans", "zjt")
    set.+=("zx")
    println(set)
  }

  def testMap() {
    var map = Map[Int, String]()
    map += (1 -> "an")
    map += (2 -> "zjt")
    map += (3 -> "hello")
    println(map)
    println(map(2))
  }

  def testImmutableMap() {
    val map = Map(1 -> "I", 2 -> "II", 3 -> "III")
    println(map)
    println(map(2))
  }

  //Map(2 -> zjt, 1 -> an, 3 -> hello) TODO 为什么顺序不对
  def testMutableMap() {
    val map = mutable.Map[Int, String]()
    map += (1 -> "an")
    map += (2 -> "zjt")
    map += (3 -> "hello")
    map.foreach {
      case (key, value) => println(">>> key=" + key + ", value=" + value)
    }
    println(map.keySet)
    println(map(2))
  }

  def testHashMap() {
    val map = mutable.HashMap[Int, String]()
    map += (1 -> "an")
    map += (2 -> "zjt")
    map += (3 -> "hello")
    println(map)
    println(map(2))
  }

  def main(args: Array[String]) {
    //    println("Hello, world!")
    //    testForeach()
    //    testTuple()
    //    testSet()
    //    testMutableSet()
    //    testHashSet()
    //    testMap()
    //        testMutableMap()
    //    testImmutableMap()
    //    testHashMap()
    //    def isort(xs: List[Int]): List[Int] = xs match {
    //      case List() => List()
    //      case x :: xs1 => insert(x, isort(xs1))
    //    }
    val nums = 1 :: 2 :: 3 :: 4 :: Nil
    def insert(x: Int, xs: List[Int]): List[Int] = xs match {
      case List() => List(x)
      case y :: ys => if (x <= y) x :: xs else y :: insert(x, ys)
    }
    val t = mutable.LinkedList(11, 2, 3, 8, 4)
    println(insert(13, t.toList))
  }
}
